<?php

/**
 * @Author: kendy
 * @Date:   2020-03-09 10:10:32
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2020-03-30 15:52:47
 */
namespace app\common\model;
use think\Model;
use think\Db;
use think\Loader;
use app\common\model\LogOrderModel as lo;
use app\common\model\JlouOrderModel as jl;
class BargainOrderModel extends Model
{
    // 确定链接表名
    protected $name = 'bargain_order';
    
    /**
      * [submitBargainOrder 提交砍价订单]
      * @Author   kendy
      * @DateTime 2020-03-09T010:14:10+0800
      * @return   [type]                   [description]
      */
   public function submitBargainOrder()
    {  
    	
     $address = input('post.address');
     $phone = input('post.phone');
     $name = input('post.name'); 
     $start_id = input('post.start_id'); 
     $payType = input('post.pay_type');// 1支付宝 2微信 
     $userId = input('post.uid');// 1支付宝 2微信 
     if(empty($address))
        return apiReturn('400','收货人地址为空',0);
     if(empty($phone))
        return apiReturn('400','收货人电话',0);
     if(empty($name))
        return apiReturn('400','收货人姓名为空',0); 
      if(empty($start_id))
        return apiReturn('400','用户发起砍价id为空',0); 
       if(empty($userId))
        return apiReturn('400','用户id为空',0); 
     //查看是否重复提交订单那
      $is_repetition = Db::name('bargain_order')->where(['start_id'=>$start_id,'uid'=>$userId])->find();
      if(!empty($is_repetition))
         return apiReturn('400','请勿重复提交订单',0);
      //查看该砍价是否砍完
     $bargainInfo = Db::name('bargain_start') 
     ->alias("bs")
     ->field('bs.create_time,bg.id,bg.bargain_money,bs.uid,bs.goods_id')
     ->join("bargain_goods bg",'bg.id = bs.goods_id','inner') 
     ->where(['bs.is_have'=>1,'bs.id'=>$start_id])
     ->find();    
     if(empty($bargainInfo)){
         return apiReturn('400','无此砍价信息',0);
     }
      if(strtotime($bargainInfo['create_time'])+48 * 60 *60<time()){ 
          return apiReturn('400','砍价已过期',0);
     }  
    $isFree = $bargainInfo['bargain_money'] == 0? 1 : 0;  
    $order_status = 0;
    $log_order_string = "下单成功，等待用户支付";
    if($isFree == 1){//是免费的
    	   $payType = 0;
         $order_status = 1;
         $log_order_string = "砍价免费拿商品下单成功，等待发货";
    }else{
        if($payType == 0){
           return apiReturn('400','支付方式错误',0); 
        }
    }
      Db::startTrans();
      try{ 
        $orderSn = makeOrderSn();  
      //创建订单    
        $order['order_sn'] = $orderSn; 
        $order['uid'] = $bargainInfo['uid'];
        $order['goods_id'] = $bargainInfo['goods_id'];
        $order['name'] = $name;
        $order['address'] = $address;
        $order['phone'] = $phone;
        $order['order_status'] =  $order_status;
        $order['order_price'] =  $bargainInfo['bargain_money']; 
        $order['create_time'] =  date('Y-m-d H:i:s',time());
        $order['start_id'] =  $start_id; 
        $result = $this->isUpdate(false)->save($order); 
        if(false !== $result)
        { 
        	$jl = new jl();
           //创建订单记录
            $orderId = $this->order_id; 
            $logOrder = new lo();
            $logOrder->addLogOrder($orderId,5, $bargainInfo['uid'], 0, $log_order_string);
            //吊起支付宝或是微信支付
             if($payType == 1){
                //调起支付宝接口//订单类型 订单类型 1酒店，2景点，3跟团 4捡漏 5砍价
              $data =  $jl->getAlipays($this->order_sn,$bargainInfo['uid'],5,$bargainInfo['bargain_money']);
             }
             if($payType ==2){
             	//调起微信支付//订单类型 订单类型 1酒店，2景点，3跟团 4捡漏 5砍价
             	$data = $jl->getWxpays($this->order_sn,5,$bargainInfo['uid'],$bargainInfo['bargain_money']);
             } 
             if($payType == 0){
               return apiReturn('200','订单支付成功',0); 
             }
        } 
        Db::commit();
        // return apiReturn('200','提交订单成功',$data);  
      }catch(\Exception $e){
        Db::rollback();
        return apiReturn('400', $e->getMessage(),0); 
      }

   }

}